<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="角色名称">
              <a-input v-model="queryParam.name" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="备注信息">
              <a-input v-model="queryParam.describe" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addRole.add()">新建</a-button>
<!--      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>-->
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-role ref="removeRole" :roleIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
      </a-dropdown>
      <a-dropdown  v-action:edit v-else-if="selectedRowKeys.length == 0">
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <a-button @click="nowCol = 1 - nowCol">调整顺序</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns[nowCol]"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <template slot="roleOrder" slot-scope="text, record">
        <preview-and-edit
          v-if="paeShow"
          :id="record.key"
          :default-value="text"
          :save="saveData"
          type="number"
          @ok="(val)=>record.roleOrder=val"/>
      </template>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleAuthorize(record)">授权</a>
          <a-divider type="vertical" />
          <remove-role ref="removeRole" :roleIds="record.id" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <add-role ref="addRole" @ok="handleOk" />
    <edit-role ref="editRole" @ok="handleOk"/>
    <authorize-role ref="authorizeRole" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { queryRole, updateRoleOrder } from '@/api/system/role'
import AddRole from './modules/role/AddRole'
import EditRole from './modules/role/EditRole'
import RemoveRole from './modules/role/RemoveRole'
import AuthorizeRole from './modules/role/AuthorizeRole'
import PreviewAndEdit from '@/components/PreviewAndEdit/PreviewAndEdit'

export default {
  name: 'RoleList',
  components: {
    PreviewAndEdit,
    STable,
    AddRole,
    EditRole,
    AuthorizeRole,
    RemoveRole
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      nowCol: 0,
      // 表头
      columns: [[
        {
          title: '角色名称',
          dataIndex: 'name',
          align: 'center'
        },
        {
          title: '备注信息',
          dataIndex: 'describe',
          align: 'center'
        },
        {
          title: '新建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ], [
        {
          title: '角色名称',
          dataIndex: 'name',
          align: 'center'
        },
        {
          title: '备注信息',
          dataIndex: 'describe',
          align: 'center'
        },
        {
          title: '顺序',
          dataIndex: 'roleOrder',
          align: 'center',
          scopedSlots: { customRender: 'roleOrder' },
          width: 150
        },
        {
          title: '新建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ]],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        this.paeShow = false
        return queryRole(Object.assign(parameter, queryJson))
          .then(res => {
            this.paeShow = true
            return res.result
          })
      },
      saveData: (id, val) => {
        return updateRoleOrder({ id: id, order: val }).then(res => {
          return res.message === 'success'
        }).catch(() => false)
      },
      paeShow: true,
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  // created () {
  //   this.tableOption()
  // },
  methods: {
    // tableOption () {
    //   if (!this.optionAlertShow) {
    //     this.options = {
    //       alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
    //       rowSelection: {
    //         selectedRowKeys: this.selectedRowKeys,
    //         onChange: this.onSelectChange
    //       }
    //     }
    //     this.optionAlertShow = true
    //   } else {
    //     this.options = {
    //       alert: false,
    //       rowSelection: null
    //     }
    //     this.optionAlertShow = false
    //   }
    // },
    handleAuthorize (record) {
      this.$refs.authorizeRole.authorize(record.id)
    },
    handleEdit (record) {
      this.$refs.editRole.edit(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
